package com.mms.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.mms.entity.Department;
import com.mms.entity.Staff;
import com.mms.util.HibernateUtil;

public class PersonalInfoDao {
        public boolean modifyPersonalInfo(Staff staff) {
            Session session= HibernateUtil.getSession();
            try{ 
                Transaction transaction=session.beginTransaction();
                Staff staffToSave=(Staff)session.get(Staff.class, staff.getSId());
                staffToSave.setSName(staff.getSName());
                staffToSave.setPhone(staff.getPhone());
                staffToSave.setSex(staff.getSex());
                staffToSave.setDepartment(staff.getDepartment());
                if(staff.getPassword()!=null&&!staff.getPassword().equals("")){
                staffToSave.setPassword(staff.getPassword());
                }
                session.save(staffToSave);
                transaction.commit();
                return true;
                }catch(Exception ex){ 
                    ex.printStackTrace();
                    return false;
                }finally{ 
                session.close(); 
                } 
        }
        
        public Staff getPersonalInfo(int sID) {
            Session session= HibernateUtil.getSession();
            List<Object[]> objs;
            Staff staff=new Staff();
            try {
                String sql="select sex,position,de_name,de_id,phone,s_name from staff natural join department where s_id = ?";
                Transaction transaction=session.beginTransaction();
                Query query=session.createSQLQuery(sql);
                query.setInteger(0, sID);
                objs=query.list();
                for (Object[] objects : objs) {
                    Department department=new Department();
                    department.setDeName(objects[2].toString());
                    department.setDeId(Integer.parseInt(objects[3].toString()));
                    staff.setSex(Integer.parseInt(objects[0].toString()));
                    staff.setPosition(objects[1].toString());
                    staff.setDepartment(department);
                    staff.setPhone(objects[4].toString());
                    staff.setSName(objects[5].toString());
                }
                transaction.commit();
            } catch (Exception e) {
            e.printStackTrace();
            }
            finally{
                session.close();
            }
            return staff;
        }
        
        public String getStaffPassword(int sID) {
            Session session= HibernateUtil.getSession();
            List<String> strings;
            String pwd="";
            try {
                Transaction transaction=session.beginTransaction();
                String sql="select password from staff where s_id = ?";
                Query query=session.createSQLQuery(sql);
                query.setInteger(0, sID);
                strings=query.list();
                pwd=strings.get(0);
                transaction.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                session.close();
            }
            return pwd;
            
        }
}
